$(function () {
    $(".title a").click(function () {
        if ($(this).index() == 0) {
            $(".telLogin").hide();
            $(".accountLogin").show();
            $(".title a").removeClass("titleOn");
            $(this).addClass("titleOn");
        }
        if ($(this).index() == 2) {
            $(".telLogin").show();
            $(".accountLogin").hide();
            $(".title a").removeClass("titleOn");
            $(this).addClass("titleOn");
        }
    });
})

$('#mpanel4').slideVerify({
    type: 2,		//类型
    vOffset: 5,	//误差量，根据需求自行调整
    vSpace: 5,	//间隔
    imgName: ['1.jpg', '2.jpg'],
    imgSize: {
        width: '400px',
        height: '200px',
    },
    blockSize: {
        width: '40px',
        height: '40px',
    },
    barSize: {
        width: '400px',
        height: '40px',
    },
    ready: function () {
    },
    success: function () {
        $("#verify").css("display", "none");
        user = $("#mobile2").val();
        pd = $("#password").val();
        var url = "/login"; // 接口 
        var params = { //HTMl数据格式
            'username': user,
            'password': pd,
        };
        $.ajax({
            type: "post",
            url: "http://lex.itdida.com/itdida-api/login",
            contentType: "application/x-www-form-urlencoded",
            data: params,
            dataType: 'json',
            success: function (data) {
                if (data.statusCode == 200) {
                    sessionStorage.setItem('token', data.data);
                    window.location.href = "Home.html";
                }
            },
            error: function (data) {
                var msg = jQuery.parseJSON(data.responseText).data;
                layer.msg(msg, {
                    anim: 6
                });
                var timeoutID;
                function slowAlert() {
                    location.reload();
                }
            }
        })
    },
    error: function () {

    }
});

var user;
var pd;
$("#login_button2").click(function () {
    login("login_button2")
})
$("#login_button1").click(function () {
    login("login_button1")
})
function login(ID) {
    user = $("#mobile2").val();
    user2 = $("#mobile1").val();
    switch (ID) {
        case "login_button1":
            var username2 = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-7|9])|(?:5[0-3|5-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1|8|9]))\d{8}$/;
            if (!(username2.test(user2))) {
                layer.msg('手机号码格式不正确');
            }
            break;
        case "login_button2":
            var username = /^([a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*)$|((?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-7|9])|(?:5[0-3|5-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1|8|9]))\d{8})$/;
            if (!(username.test(user))) {
                layer.msg('手机号码或邮箱格式不正确');
            } else {
                $("#verify").show();
            }
            break;
    }
}

$(function () {
    initView();
    $("#login_button2").click(function () {
        if ($(".save_account").is(":checked")) {
            setCookie("cookie_account", $("#mobile2").val());
            setCookie("cookie_password", $("#password").val());
            setCookie("rememberMe", true);
        } else {
            delCookie("cookie_account");
            delCookie("cookie_password");
            delCookie("rememberMe");
        }
    });
});

function initView() {
    if (getCookie("cookie_account")) {
        $("#mobile2").val(getCookie("cookie_account"));
    }
    if (getCookie("cookie_password")) {
        $("#password").val(getCookie("cookie_password"));
    }
    if (getCookie("rememberMe")) {
        $(".save_account").attr("checked", getCookie("rememberMe"));
    }
    $("#mobile2").focus(function () {
        this.select();
    });
    $("#password").focus(function () {
        this.select();
    });
}

//记住密码
/**
* 写入cookie
* @param name  cookie 名
* @param value  cookie 值
*/
function setCookie(name, value) {
    var Days = 30; //此 cookie 将被保存 30 天
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
/**
* 删除cookie
* @param name
*/
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}
/**
* 读取cookie
* @param name
* @returns
*/
function getCookie(name) {
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null)
        return unescape(arr[2]);
    return null;
}